﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using Public;

namespace Dal
{
    public class News
    {
        public static DataGroup MULTIPLE(string strCon, int pageSize, int pageIndex, string whereStr)
        {
            DataTable dt = new DataTable();
            dt.Locale = CultureInfo.InvariantCulture;
            int countRow = 0;
            int countPage = 0;
            SqlParameter[] pars = new SqlParameter[5];
            pars[0] = new SqlParameter("@PageSize", SqlDbType.Int);
            pars[0].Value = pageSize;
            pars[1] = new SqlParameter("@PageIndex", SqlDbType.Int);
            pars[1].Value = pageIndex;
            pars[2] = new SqlParameter("@WhereStr", SqlDbType.NVarChar, 1000);
            pars[2].Value = whereStr;
            pars[3] = new SqlParameter("@CountRow", SqlDbType.Int);
            pars[3].Direction = ParameterDirection.Output;
            pars[4] = new SqlParameter("@CountPage", SqlDbType.Int);
            pars[4].Direction = ParameterDirection.Output;
            try
            {
                dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_News_Search_MULTIPLE", pars);
                countRow = System.Convert.ToInt32(pars[3].Value, CultureInfo.InvariantCulture);
                countPage = System.Convert.ToInt32(pars[4].Value, CultureInfo.InvariantCulture);
            }
            catch
            {
                countRow = 0;
                countPage = 0;
                throw;
            }
            return new DataGroup(dt, countRow, countPage);
        }

        public static DataTable Single(string strCon, int pkid)
        {
            DataTable dt = new DataTable();
            dt.Locale = CultureInfo.InvariantCulture;
            SqlParameter[] pars = new SqlParameter[1];
            pars[0] = new SqlParameter("@PKID", SqlDbType.Int);
            pars[0].Value = pkid;

            try
            {
                dt = SqlHelper.ExecuteDataTable(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_News_SINGLE_PKID", pars);
            }
            catch
            {
                throw;
            }
            return dt;
        }

        public static int Insert(string strCon, string txbNewsTitle,
                string txbNewsContent,
                string txbKeywords,
                string txbDescription)
        {
            int rtn = 0;
            SqlParameter[] pars = new SqlParameter[4];
            pars[0] = new SqlParameter("@NewsTitle", SqlDbType.NVarChar, 20);
            pars[0].Value = txbNewsTitle;
            pars[1] = new SqlParameter("@NewsContent", SqlDbType.NVarChar);
            pars[1].Value = txbNewsContent;
            pars[2] = new SqlParameter("@Keywords", SqlDbType.NVarChar);
            pars[2].Value = txbKeywords;
            pars[3] = new SqlParameter("@Description", SqlDbType.NVarChar);
            pars[3].Value = txbDescription;
            try
            {
                rtn = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_News_INSERT", pars);
            }
            catch
            {
                throw;
            }
            return rtn;
        }

        public static int Update(string strCon,
                string txbNewsTitle,
                string txbNewsContent,
                string txbKeywords,
                string txbDescription,
                int txbPKID)
        {
            int rtn = 0;
            SqlParameter[] pars = new SqlParameter[5];
            pars[0] = new SqlParameter("@NewsTitle", SqlDbType.NVarChar, 20);
            pars[0].Value = txbNewsTitle;
            pars[1] = new SqlParameter("@NewsContent", SqlDbType.NVarChar);
            pars[1].Value = txbNewsContent;
            pars[2] = new SqlParameter("@Keywords", SqlDbType.NVarChar);
            pars[2].Value = txbKeywords;
            pars[3] = new SqlParameter("@Description", SqlDbType.NVarChar);
            pars[3].Value = txbDescription;
            pars[4] = new SqlParameter("@PKID", SqlDbType.Int);
            pars[4].Value = txbPKID;

            try
            {
                rtn = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_News_UPDATE", pars);
            }
            catch
            {
                throw;
            }
            return rtn;
        }

        public static int Delete(string strCon, int pkid)
        {
            int rtn = 0;
            SqlParameter[] pars = new SqlParameter[1];
            pars[0] = new SqlParameter("@PKID", SqlDbType.SmallInt);
            pars[0].Value = pkid;
            try
            {
                rtn = SqlHelper.ExecuteNonQuery(DataProtector.DecryptString(strCon), CommandType.StoredProcedure, "SP_News_DELETE", pars);
            }
            catch
            {
                throw;
            }
            return rtn;
        }
    }
}
